package com.source.jucutils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author wengqy
 * @Date 2021/1/20
 */
public class WaitDemo {

    public static void main(String[] args) {

            String  name =null ;
         new Thread(new Cons(name)).start();
         new Thread(new Proc(name)).start();


    }
}



class   Cons  implements   Runnable{
    private String name="";
    public Cons(String name) {
        this.name = name;
    }



    @Override
    public void run() {

        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while(true){
            put( );
        }



    }


    public  synchronized   void  put( ){
        if( name == null){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        } else {
            System.out.println("消费者消费："+name);
            name=null;
           notifyAll();
        }

    }
}





class   Proc  implements   Runnable{
    private String name;
    public Proc(String name) {
        this.name = name;
    }



    @Override
    public void run() {

        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while(true){
            put( );
        }
     }


     public  synchronized   void  put( ){
           if(name!=null){
               try {
                   wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }

           } else {
               name="hi Thirday";
               System.out.println("生产者生产："+name);
               notifyAll();
           }

     }
}
